package com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.repository;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.iwhalecloud.citybrain.flow.platform.manager.domain.model.FlowMessageConfigVo;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowMessageConfigRepository;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowMessageConfig;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.entities.FlowMessageConfigExample;
import com.iwhalecloud.citybrain.flow.platform.manager.infrastructure.mapper.FlowMessageConfigDAO;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author lilh
 * @date 2019-09-24 15:16
 */
@Service
public class FlowMessageConfigRepositoryImpl implements FlowMessageConfigRepository {

    @Autowired
    private FlowMessageConfigDAO flowMessageConfigDAO;

    @Override
    public int insertMessageConfig(FlowMessageConfigVo vo) {
        vo.setCreateTime(new Date());
        vo.setUpdateTime(new Date());
        FlowMessageConfig entity = convert(vo);
        flowMessageConfigDAO.insertSelective(entity);
        return entity.getId();
    }

    @Override
    public List<FlowMessageConfigVo> getConfigByChannel(String channel) {
        if (StringUtils.isBlank(channel)) {
            return Collections.emptyList();
        }
        FlowMessageConfigExample query = new FlowMessageConfigExample();
        query.createCriteria().andChannelCodeEqualTo(channel);
        return flowMessageConfigDAO.selectByExample(query).stream().map(this::convert).collect(Collectors.toList());
    }

    @Override
    public FlowMessageConfigVo getConfigByChannelAndSystemCode(String channel, String systemCode) {
        FlowMessageConfigExample query = new FlowMessageConfigExample();
        FlowMessageConfigExample.Criteria criteria = query.createCriteria().andChannelCodeEqualTo(channel);
        if (StringUtils.isNotBlank(systemCode)) {
            criteria.andSystemCodeEqualTo(systemCode);
        }
        List<FlowMessageConfig> flowMessageConfigs = flowMessageConfigDAO.selectByExample(query);
        if (CollectionUtils.isNotEmpty(flowMessageConfigs)) {
            return convert(flowMessageConfigs.get(0));
        }
        return null;
    }

    public List<FlowMessageConfig> getConfigByExample(FlowMessageConfigExample query) {
        return flowMessageConfigDAO.selectByExample(query);
    }

    private FlowMessageConfig convert(FlowMessageConfigVo entity) {
        if (Objects.isNull(entity)) {
            return null;
        }
        FlowMessageConfig result = new FlowMessageConfig();
        BeanUtils.copyProperties(entity, result);
        return result;
    }

    private FlowMessageConfigVo convert(FlowMessageConfig entity) {
        if (Objects.isNull(entity)) {
            return null;
        }
        FlowMessageConfigVo result = new FlowMessageConfigVo();
        BeanUtils.copyProperties(entity, result);
        return result;
    }
}
